package com.ranyk.www.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * CLASS_NAME: SwaggerConfig.java <br/>
 *
 * @author ranyk           <br/>
 * @version V1.0           <br/>
 * @decription: Swagger 配置类  <br/>
 * @date: 2022-04-14 <br/>
 */
@Configuration
public class SwaggerConfig {

    @Value("${swagger.version}")
    private String version;
    @Value("${swagger.title}")
    private String title;
    @Value("${swagger.description}")
    private String description;
    @Value("${swagger.server_url}")
    private String serverUrl;
    @Value("${swagger.contact.name}")
    private String name;
    @Value("${swagger.contact.url}")
    private String url;
    @Value("${swagger.contact.email}")
    private String email;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .pathMapping("/")
                .enable(true)
                .apiInfo(apiInfo())
                .host(this.serverUrl)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(this.title)
                .termsOfServiceUrl(this.serverUrl)
                .description(this.description)
                .contact(new Contact(this.name, this.url, this.email))
                .version(this.version)
                .build();
    }

}
